package internal

import "fmt"

func BinarySearch(list []int, target int) *int {
	left, right := 0, len(list)-1
	// go中，整数除法，默认向下取整么？是的
	for len(list[left:right]) > 0 {
		index := (left + right) / 2
		value := list[index]
		fmt.Printf("left :%+v, right: %+v\n", left, right)
		fmt.Printf("index: %+v, value: %+v\n", index, value)
		if value > target {
			right = index
		} else if value < target {
			left = index
		} else if value == target {
			return &index
		} else if len(list[left:right]) == 1 && value != target {
			return nil
		}
	}
	return nil
}
